package com.ningxun.pagent.flow.handler;

import com.ningxun.pagent.common.core.domain.entity.SysRole;
import com.ningxun.pagent.common.core.domain.entity.SysUser;
import com.ningxun.pagent.common.utils.SecurityUtils;
import com.ningxun.pagent.system.mapper.SysRoleMapper;
import com.ningxun.pagent.system.mapper.SysUserMapper;
import org.dromara.warm.flow.core.handler.PermissionHandler;
import org.dromara.warm.flow.core.utils.StreamUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 办理人权限处理器（可通过配置文件注入，也可用@Bean/@Component方式）
 *
 * @author shadow
 */
@Component
/**
 * 办理人权限处理器（可通过配置文件注入，也可用@Bean/@Component方式）
 *
 * @author shadow
 */
public class CustomPermissionHandler implements PermissionHandler {

    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private SysRoleMapper roleMapper;
    /**
     * 获取当前操作用户所有权限
     */
    @Override
    public List<String> permissions() {
        // 办理人权限标识，比如用户，角色，部门等, 流程设计时未设置办理人或者ignore为true可不传 [按需传输]
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        List<SysRole> roles = sysUser.getRoles();
        List<String> permissionList = StreamUtils.toList(roles, role -> "role:" + role.getRoleId());
        if (sysUser.getUserId() != null) {
            permissionList.add(String.valueOf(sysUser.getUserId()));
        }
        if (sysUser.getDeptId() != null) {
            permissionList.add("dept:" + sysUser.getDeptId());
        }
        return permissionList;
    }

    /**
     * 获取当前办理人
     * @return 当前办理人
     */
    @Override
    public String getHandler() {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        if (sysUser.getUserId() != null) {
            return String.valueOf(sysUser.getUserId());
        }
        return null;
    }

    /**
     * 转换办理人，比如设计器中预设了能办理的人，如果其中包含角色或者部门id等，可以通过此接口进行转换成用户id
     */
    @Override
    public List<String> convertPermissions(List<String> permissions) {
        // 把角色部门转换成用户
        //TODO 我们在做设置人员等信息时，是通过角色来进行的
        //如果前面带有role:
        List<String> result = new ArrayList<>();
        for (String permission : permissions){
            if (permission.startsWith("role:")){
                String substring = permission.substring(5);
                List<SysUser> sysUsers = userMapper.selectUserByRoleId(Long.valueOf(substring));
                sysUsers.stream()
                        .map(user -> user.getUserId().toString())
                        .forEach(result::add);
            }
            else if (permission.startsWith("dept:")){
                String substring = permission.substring(5);
                List<SysUser> sysUsers = userMapper.selectUserByDeptId(Long.valueOf(substring));
                sysUsers.stream()
                        .map(user -> user.getUserId().toString())
                        .forEach(result::add);
            }
        }
        return result;
//        return permissions;
    }
}
